<?php
/**
 * Created by PhpStorm.
 * User: liuwei
 * Date: 2022/12/16
 * Time: 14:19
 */

namespace App\Http\Controllers\Backend1;


use App\Http\Controllers\Backend\BaseController;
use App\Http\Requests\Company\ProductRequest;
use App\Services\Business\Models\Product;
use App\Services\Other\Models\UserPrize;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;


class ProductController extends BaseController
{


    public function index(Request $request)
    {

        $size = $request->input('size', 20);
        $keywords = $request->input('keywords', '');
        $products = Product::query()
            ->when($keywords, function ($query) use ($keywords) {
                return $query->where('title', 'like', '%' . $keywords . '%');
            })
            ->orderByDesc('id')
            ->paginate($size);

        return $this->successData(compact('products'));
    }

    //资源的试卷列表

    public function store(ProductRequest $request, Product $product)
    {
        $data = $request->filldata();
        $product->fill($data)->save();
        //更新主图
        $data['images'] && $product->mediaImages()->attach($data['images']);
        //更新主视频
        $data['video'] && $product->mediaVideo()->attach($data['video']);
        return $this->success("插入产品成功");
    }


    public function detail(Request $request, $id)
    {

        $product = Product::query()
            ->with(['mediaVideo', 'mediaImages'])
            ->where('id', $id)
            ->firstOrFail();
        return $this->successData(compact("product"));

    }

    public function edit(Request $request, $id)
    {

        $product = Product::query()
            ->with(['mediaVideo', 'mediaImages'])
            ->where('id', $id)
            ->firstOrFail();
        return $this->successData(compact("product"));

    }

    public function delete(Request $request, $id)
    {
        $product = Product::query()->where('id', $id)->firstOrFail();
        $product->delete();
        return $this->success(__('删除产品成功'));
    }

    public function batchDelete(Request $request)
    {
        $ids = $request->input('ids');

        $products =  Product::query()->whereIn('id', $ids)->get();
        DB::transaction(function () use ($products) {
            foreach ($products as $product) {

                $product->mediaImages()->delete();
                $product->mediaVideo()->delete();
                $product->delete();
            }
        });

        return $this->success(__('删除试题成功'));
    }

    public function update(ProductRequest $request, $id)
    {
        $data =$request->filldata();
        $product = Product::query()->where('id', $id)->firstOrFail();
        DB::transaction(function () use ($product,$data) {

            $product->fill($data)->update();
            $product->mediaImages()->sync($data['images']);
            $product->mediaVideo()->sync($data['video']);

        });
        return $this->success(__('更新产品成功'));

    }

    public function redeem(Request $request)
    {

        $productId = $request->input('product_id');
        $quantity = $request->input('quantity');

        $product = Product::find($productId);

        // 检查用户的积分余额是否足够兑换指定的商品
        $user = Auth::user();
        $requiredPoints = $product->points * $quantity;
        if ($user->point < $requiredPoints) {
            return $this->error(__('积分不够兑换,继续努力'));
        }

        $user->point -= $requiredPoints;
        $user->save();

        UserPrize::create([
            'user_id' => Auth::id(),
            'product_id' => $productId,
            'quantity' => $quantity,
            'point'=>$product->point,
            'remark'=>'积分兑换'
        ]);
        //更新产品库存
        $product->decrement('stock',$quantity);

        // 返回成功响应
        return $this->success(__('兑换成功成功'));
    }





}